package cn.edu.lzu.unitweb.controller;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.entity.Role;
import cn.edu.lzu.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: cn.edu.lzu.unitweb.controller
 * @ClassName: RoleController
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/5/6 10:52
 * @Version: 1.0
 */
@Controller
public class RoleController {
    @Resource
    private RoleService roleService;

    @RequestMapping("/getRoles")
    @ResponseBody
    public DataPage getRoles() {
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        return roleService.getRoles(examiner.getUnitId());
    }

    @ResponseBody
    @RequestMapping("/batchDeleteRolesById")
    public ResponseResult batchDeleteRolesById(String roleIds) {
        return roleService.batchDeleteRolesById(roleIds);
    }

    @ResponseBody
    @RequestMapping("/executeAddRole")
    public ResponseResult addRole(String role, String roleName, Integer personalCenter, Integer securitySetting, Integer logout, Integer peopleManage, Integer medExamine, Integer permissionManage, Integer itemSetting, Integer packageSetting, Integer registe, Integer charge, Integer fillInfo, Integer totalExamine, Integer labExamine, Integer radExamine, Integer speExamine, Integer supExamine, Integer cliExamine) {
        System.out.println("role = " + role + ", roleName = " + roleName + ", personalCenter = " + personalCenter + ", securitySetting = " + securitySetting + ", logout = " + logout + ", peopleManage = " + peopleManage + ", medExamine = " + medExamine + ", permissionManage = " + permissionManage + ", itemSetting = " + itemSetting + ", packageSetting = " + packageSetting + ", registe = " + registe + ", charge = " + charge + ", fillInfo = " + fillInfo + ", totalExamine = " + totalExamine + ", labExamine = " + labExamine + ", radExamine = " + radExamine + ", speExamine = " + speExamine + ", supExamine = " + supExamine + ", cliExamine = " + cliExamine);
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        String perms="";
        if(personalCenter!=null)
            perms=perms+personalCenter+",";
        if(securitySetting!=null)
            perms=perms+securitySetting+",";
        if(logout!=null)
            perms=perms+logout+",";
        if(peopleManage!=null)
            perms=perms+peopleManage+",";
        if(medExamine!=null)
            perms=perms+medExamine+",";
        if(permissionManage!=null)
            perms=perms+permissionManage+",";
        if(itemSetting!=null)
            perms=perms+itemSetting+",";
        if(packageSetting!=null)
            perms=perms+packageSetting+",";
        if(registe!=null)
            perms=perms+registe+",";
        if(charge!=null)
            perms=perms+charge+",";
        if(totalExamine!=null)
            perms=perms+totalExamine+",";
        if(labExamine!=null)
            perms=perms+labExamine+",";
        if(supExamine!=null)
            perms=perms+supExamine+",";
        if(speExamine!=null)
            perms=perms+speExamine+",";
        if(cliExamine!=null)
            perms=perms+cliExamine+",";
        if(radExamine!=null)
            perms=perms+radExamine+",";
        if(fillInfo!=null)
            perms=perms+fillInfo+",";
        ResponseResult responseResult=roleService.addRole(examiner.getUnitId(), role, roleName, perms);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/deleteRoleById")
    public ResponseResult deleteRoleById(Integer roleId){
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        ResponseResult responseResult=roleService.deleteRoleById(examiner.getUnitId(), roleId);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/getRoleById")
    public Map<String, Object> getRoleById(Integer roleId){
        System.out.println("roleId = " + roleId);
        Map<String, Object> map=roleService.editRole(roleId);
        System.out.println(map);
        return  map;
    }

    @ResponseBody
    @RequestMapping("/getRolesByExaminerId")
    public Map<String, Object> getRolesByExaminerId(Long examinerId){
        System.out.println("examinerId = " + examinerId);
        Map<String, Object> map=roleService.getRolesAndExaminerRolesByExaminerId(examinerId);
        System.out.println(map);
        return map;
    }

    @ResponseBody
    @RequestMapping("/executeEditRole")
    public ResponseResult executeEditRole(Role role, Integer personalCenter, Integer securitySetting, Integer logout, Integer peopleManage, Integer medExamine, Integer permissionManage, Integer itemSetting, Integer packageSetting, Integer registe, Integer charge, Integer fillInfo, Integer totalExamine, Integer labExamine, Integer radExamine, Integer speExamine, Integer supExamine, Integer cliExamine){
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        String perms="";
        if(personalCenter!=null)
            perms=perms+personalCenter+",";
        if(securitySetting!=null)
            perms=perms+securitySetting+",";
        if(logout!=null)
            perms=perms+logout+",";
        if(peopleManage!=null)
            perms=perms+peopleManage+",";
        if(medExamine!=null)
            perms=perms+medExamine+",";
        if(permissionManage!=null)
            perms=perms+permissionManage+",";
        if(itemSetting!=null)
            perms=perms+itemSetting+",";
        if(packageSetting!=null)
            perms=perms+packageSetting+",";
        if(registe!=null)
            perms=perms+registe+",";
        if(charge!=null)
            perms=perms+charge+",";
        if(totalExamine!=null)
            perms=perms+totalExamine+",";
        if(labExamine!=null)
            perms=perms+labExamine+",";
        if(supExamine!=null)
            perms=perms+supExamine+",";
        if(speExamine!=null)
            perms=perms+speExamine+",";
        if(cliExamine!=null)
            perms=perms+cliExamine+",";
        if(radExamine!=null)
            perms=perms+radExamine+",";
        if(fillInfo!=null)
            perms=perms+fillInfo+",";
        ResponseResult responseResult=roleService.executeEditRole(role, perms);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/getRoleByExaminerId")
    public List<Role> getRoleByExaminerId(Long examinerId){
        List<Role> roleList=roleService.getRolesByExaminerId(examinerId);
        return roleList;
    }

    @ResponseBody
    @RequestMapping("/executeApportionRole")
    public ResponseResult executeApportionRole(Long examinerId, String roleIds){
        System.out.println("examinerId = " + examinerId + ", roleIds = " + roleIds);
        ResponseResult responseResult=roleService.executeApportionRole(examinerId, roleIds);
        return responseResult;
    }
}
